package com.jh.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.jh.pojo.IPCamMsg;
import com.jh.pojo.RadarMsg;
import com.jh.pojo.RadarRetToBsBean;
import com.jh.pojo.SPInfo;
import com.jh.utils.CacheUtils;

/**
 * Servlet implementation class DisplayRadarPanelServlet
 */
@WebServlet(description = "Display Radar Panel", urlPatterns = { "/DisplayRadarPanelServlet" })
public class DisplayRadarPanelServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    static final Logger logger = LoggerFactory.getLogger(DisplayRadarPanelServlet.class);

    Gson g = new Gson();

    CacheUtils cacheUtils = CacheUtils.getInstance();

    /**
     * @see HttpServlet#HttpServlet()
     */
    public DisplayRadarPanelServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset:utf-8");
        String ipAddr = request.getParameter("ipaddr");
        System.out.println("ipAddr: " + ipAddr);
        logger.debug("--->" + "ipAddr: " + ipAddr);

        SPInfo spInfo = (SPInfo) cacheUtils.query(ipAddr);
        if (spInfo == null) {

            logger.debug("--->" + " spInfo == null ");
            return;
        }
        RadarMsg radarMsg = spInfo.getRadarMsg();

        if (radarMsg == null) {

            logger.debug("--->" + " radarMsg == null ");
            return;
        }

        IPCamMsg ipCamMsg = spInfo.getIpCamMsg();

        if (ipCamMsg == null) {

            logger.debug("--->" + " ipCamMsg == null ");
            return;
        }

        RadarRetToBsBean radarRetToBsBean = new RadarRetToBsBean();
        radarRetToBsBean.setIp(radarMsg.getRadarIP());
        radarRetToBsBean.setThreshold(radarMsg.getrSwivelThresh());
        radarRetToBsBean.setThreshStatus(radarMsg.getrSwivelThreshStatus());
        radarRetToBsBean.setSpeed(radarMsg.getrSwivelResideSpd());
        radarRetToBsBean.setPosAngle(radarMsg.getrSwivelResidePos());
        radarRetToBsBean.setSwiveResideStatus(radarMsg.getrSwivelResideStatus());
        radarRetToBsBean.setSpeed2(radarMsg.getrSwivelCtrlSpd());
        radarRetToBsBean.setDirec(radarMsg.getrSwivelCtrlDir());
        radarRetToBsBean.setSwiveControlStatus(radarMsg.getrSwivelCtrlStatus());
        radarRetToBsBean.setLedCmd(radarMsg.getrLedCmd());
        radarRetToBsBean.setLedCmdStatus(radarMsg.getrLedCmdStatus());
        radarRetToBsBean.setCurrentAngle(radarMsg.getSysAziAngle());
        radarRetToBsBean.setIpcamName(ipCamMsg.getIpcamName());
        radarRetToBsBean.setIpcamZMLURL(ipCamMsg.getIpcamZMLURL());
        String jsonData = g.toJson(radarRetToBsBean);
        System.out.println("1 DisplayRadarPanelServlet---->" + radarRetToBsBean.toString());
        System.out.println("2 DisplayRadarPanelServlet---->" + jsonData);
        PrintWriter writer = response.getWriter();
        writer.print(jsonData);
        writer.flush();
        writer.close();
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
